package com.weed.table.renderer;

import com.weed.table.BaseTable;
import java.awt.Color;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JTable;

import java.awt.Component;
import java.text.Format;
import java.util.Formatter;
import java.util.HashMap;
import javax.swing.UIManager;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;


/*** 函数说明：
 *     表格黙认的绘制者。
 * 作者     日期           说明
 * 卓志成    2010-11-19    增加自动最大化.
 *
 */
public class ColorRenderer extends DefaultTableCellRenderer {
    // 渐近行的颜色设置.
    protected Color oneColor = new Color(247, 247, 239);
    protected Color twoColor = new Color(235, 241, 226);
    protected Color threeColor = new Color(231, 239, 222);
    //设置指定行的颜色。
    protected Color rowColor = null;
    protected int row = -1;
    protected double maxWidth= 0d;
    public HashMap map = new java.util.HashMap();
    public ColorRenderer() {
        
    }
    /**
     * 设置行的颜色。
     * @param row int
     * @param rowColor Color
     */
    public void setRowColor(int row ,Color rowColor){
        this.row = row;
        this.rowColor = rowColor;
    }

    /**
     * 绘制的对象。
     * @param table JTable
     * @param value Object
     * @param isSelected boolean
     * @param hasFocus boolean
     * @param row int
     * @param column int
     * @return Component
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected,
            boolean hasFocus, int row,
            int column) {
        this.setHorizontalAlignment(this.CENTER);
        if (isSelected) {
            super.setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        } else {
            int i = row % 3;
            switch (i) {
            case 2:
                setBackground(threeColor);
                break;
            case 1:
                setBackground(twoColor);
                break;
            case 0:
                setBackground(oneColor);
                break;
            case -1:
                setBackground(table.getBackground());
                break;
            }
            super.setForeground(table.getForeground());
        }
        if (this.row != -1 && this.row == row) {
            setBackground(rowColor);
        }
        setFont(table.getFont());
        if (hasFocus) {
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        } else {
            setBorder(noFocusBorder);
        }
        //获得格式化的列。
        if(table instanceof BaseTable){
           Format format = ((BaseTable) table).getColumnFormatter(column);
           if (format != null && value != null) {
                try {
                    setValue(format.format(value));
                } catch (Exception ex) {
                    setValue(value);
                }
            } else
                setValue(value);
        }
        TableColumnModel columnModel = table.getColumnModel();
        //获得最大的宽度
        Double maxPreSize = (Double)map.get(column);
        if(maxPreSize== null){
            map.put(column,new Double(this.getPreferredSize().getWidth()));
        }else {
            double maxWidth = this.getPreferredSize().getWidth();
            maxWidth = Math.max(maxPreSize, maxWidth);
            map.put(column,new Double(maxWidth));
        }
        TableColumn tablecolumn = columnModel.getColumn(column);
        tablecolumn.setPreferredWidth((int)maxPreSize.doubleValue());
        return this;
    }

}
